const express = require('express');
const static = require('express-static');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');

const consolidate = require('consolidate');
const bodyParser = require('body-parser');
const multer = require('multer');

var server = express();
server.listen(8081);

//cookie
server.use(cookieParser('abclsjdd'));
(function(){
    //session
    //秘钥
    var keys = [];
    for (var i = 0; i < 100000; i++) {
        keys.push('blog_session_' + Math.random());
    }
    server.use(cookieSession({
        keys: keys,
        name: 'blog',
        maxAge: 20 * 60 * 1000
    }));

})();

//post
server.use(bodyParser.urlencoded({ extended: false }));

//文件上传
server.use(multer({ dest: "./static/upload" }).any());

/*配置模板文件*/
server.set('view engine', 'html');
server.set('views', './templates');
server.engine('html', consolidate.ejs);


//处理根目录请求
server.use('/', require('./routes/home'));

//处理后台请求
server.use('/admin', require('./routes/admin'));

//使用静态数据
server.use(static('./static'));


